package com.example.maps;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.GoogleMap.OnMarkerDragListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

/**
 * 
 * @author Luis
 *
 */

public class MainActivity extends Activity {

	private FragmentManager fragmentManager;
	private MapFragment mapFragment;
	private GoogleMap googleMap;
	private LatLng sfLatLng;
	private EditText latitude;
	private EditText longitude;
	private Button go;
	private RadioGroup all;
	private RadioButton normal;
	private RadioButton sattelitep;
	private RadioButton hybrid;

	@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
 
        // Checks the orientation of the screen for landscape and portrait and set portrait mode always
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            
            setContentView(R.layout.activity_main_h);
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            setContentView(R.layout.activity_main);
        }
        
       /* Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
        int orientation = display.getOrientation(); 
         switch(orientation) {
            case Configuration.ORIENTATION_PORTRAIT:
                setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                break;
            case Configuration.ORIENTATION_LANDSCAPE:
                setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                break;                   
        }*/
        
        
    }
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		int available = GooglePlayServicesUtil
				.isGooglePlayServicesAvailable(getApplicationContext());

		if (available == 0) {
			Toast.makeText(getApplicationContext(),
					" Google Maps is Available in your Mobile ",
					Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(getApplicationContext(),
					" Google Maps is not Available in your Mobile ",
					Toast.LENGTH_SHORT).show();
		}
		
		this.go = (Button)findViewById(R.id.button1);
		this.latitude = (EditText)findViewById(R.id.latitude);
		this.longitude = (EditText)findViewById(R.id.longitude);
		
		this.fragmentManager = getFragmentManager();
		this.mapFragment = (MapFragment) this.fragmentManager
				.findFragmentById(R.id.map);
		this.googleMap = this.mapFragment.getMap();

		this.all = (RadioGroup) findViewById(R.id.radioGroup1);
		
		
		 GPSTracker gps = new GPSTracker(this);
	        if(!gps.canGetLocation()){ 
	        	gps.showSettingsAlert();        	
	        } else {
	        	double latitude1 = gps.getLatitude();
	            double longitude1 = gps.getLongitude();
	            this.latitude.setText(String.valueOf(latitude1));
	    		this.longitude.setText(String.valueOf(longitude1));
	            // \n is for new line
	            Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();    
	        	
	        }
		
		

		all.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				int selectedId = group.getCheckedRadioButtonId();
				
				
				Toast.makeText(getApplicationContext(), "selectedId = " + selectedId,
						Toast.LENGTH_LONG).show();
				
				switch (selectedId) {
				case R.id.radio0:
					normal = (RadioButton) findViewById(selectedId);
					Toast.makeText(getApplicationContext(), "0",
							Toast.LENGTH_LONG).show();
					break;
				case R.id.radio2:
					sattelitep = (RadioButton) findViewById(selectedId);
					Toast.makeText(getApplicationContext(), "2",
							Toast.LENGTH_LONG).show();
					break;
				case R.id.radio1:
					hybrid = (RadioButton) findViewById(selectedId);
					Toast.makeText(getApplicationContext(), "1",
							Toast.LENGTH_LONG).show();
					break;
				default: 
					Toast.makeText(getApplicationContext(), "Nada",
							Toast.LENGTH_LONG).show();
					return;
				}
				
				
				if (normal != null && normal.isChecked()) {
					googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
				}
				if (sattelitep != null && sattelitep.isChecked()) {
					googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
				}
				if (hybrid != null && hybrid.isChecked()) {
					googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
				}
			}
		});
		
		
		go.setOnClickListener(new View.OnClickListener() {

			@SuppressLint("UseValueOf")
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
								
				sfLatLng = new LatLng(
						new Double(latitude.getText().toString()), new Double(
								longitude.getText().toString()));
				
				googleMap.addMarker(new MarkerOptions()
						.position(sfLatLng)
						.title("title")
						.snippet("snippet")
						.icon(BitmapDescriptorFactory
								.defaultMarker(BitmapDescriptorFactory.HUE_CYAN)).draggable(true));
				
				googleMap.getUiSettings().setCompassEnabled(true);
				googleMap.getUiSettings().setZoomControlsEnabled(true);
				googleMap.getUiSettings().setMyLocationButtonEnabled(true);
				
				
				CameraPosition cameraPosition = new CameraPosition.Builder()
			    .target(sfLatLng)      // Sets the center of the map to Mountain View
			    .zoom(17)                   // Sets the zoom
			    .bearing(90)                // Sets the orientation of the camera to east
			    .tilt(90)                   // Sets the tilt of the camera to 30 degrees
			    .build();                   // Creates a CameraPosition from the builder
				googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
				
				googleMap.setOnMarkerDragListener(new OnMarkerDragListener() {
					
					@Override
					public void onMarkerDragStart(Marker marker) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onMarkerDragEnd(Marker marker) {
						// TODO Auto-generated method stub
						googleMap.addMarker(new MarkerOptions()
						.position(sfLatLng)
						.title("title")
						.snippet("snippet")
						.icon(BitmapDescriptorFactory
								.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)).draggable(true));
						
						
						CameraPosition cameraPosition = new CameraPosition.Builder()
					    .target(sfLatLng)      // Sets the center of the map to Mountain View
					    .zoom(14)                   // Sets the zoom
					    .bearing(90)                // Sets the orientation of the camera to east
					    .tilt(90)                   // Sets the tilt of the camera to 30 degrees
					    .build();                   // Creates a CameraPosition from the builder
						googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
						
						
				
					}
					
					@Override
					public void onMarkerDrag(Marker marker) {
						// TODO Auto-generated method stub
						
					}
				});
				
				googleMap.setOnMapClickListener(new OnMapClickListener() {
					
					@Override
					public void onMapClick(LatLng point) {
						// TODO Auto-generated method stub
						Toast.makeText(getApplicationContext(), "Latitud  " + point.latitude + " Longitud = " + point.longitude, Toast.LENGTH_LONG).show();
						sfLatLng = point;
					}
				});
				
				
			/*	googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
						sfLatLng, 15));*/
				

			}
		});
		
		
		

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
